<?php
/**
 * Created by PhpStorm.
 * User: Elton
 * Date: 2021/5/31
 * Time: 13:57
 */

/**
 * 冒泡排序
 * @param array $arr
 * @return array
 */
function bubbleSort(array $arr)
{
    $len = count($arr);
    for ($i = 0; $i < ($len - 1); $i++) {
        for ($j = 0; $j < ($len - 1 - $i); $j++) {
            if ($arr[$j] > $arr[$j + 1]) {
                $tmp = $arr[$j];
                $arr[$j] = $arr[$j + 1];
                $arr[$j + 1] = $tmp;
            }
        }
    }
    return $arr;
}

$a = [10, 30, 20, 50, 9, 19, 8];

print_r(bubbleSort($a));
